<?php include 'header.php';?>
    <!--其他区域-->
    <section style="padding-bottom:20px;">
        <div class="block item" style="box-shadow:none; background:#F8F9FA">
            <img id="book_img" src="<?php echo $detail_list['pic']; ?>" style="height:145px;">
            <h3 id="title" style="word-wrap: break-word;word-break: break-all;-webkit-line-clamp: 4;display: -webkit-box;-webkit-box-orient: vertical;overflow: hidden;white-space: normal;height:auto;"><?php echo $detail_list['title']; ?></h3>
            <div>
                <p><strong>索书号：</strong><?php if(isset($detail_table[0])){echo $detail_table[0]['index'];}else{echo '暂无';} ?></p>
                <p><strong>作&nbsp;&nbsp;&nbsp;者：</strong><span id="author"><?php echo $detail_list['author']; ?></span></p>
                <p style="word-wrap: break-word;word-break: break-all;-webkit-line-clamp: 2;display: -webkit-box;-webkit-box-orient: vertical;overflow: hidden;white-space: normal;height:auto;"><strong>出版社：</strong><?php echo $detail_list['press']; ?></p>
                <p style="height:32px;margin-top:10px;">
                    <?php
                        if( $is_collect == 1 ){
                            echo '<span  class="fcolor no-button judgestart" id="had_collect" onclick="collect(0)">';
                            echo '    <i class="icon iconfont icon-star1"></i>已收藏';
                            echo '</span>';
                            echo '<span  class="mcolor no-button judgestart" style="display:none;" id="no_collect" onclick="collect(1)">';
                            echo '    <i class="icon iconfont icon-star"></i>收藏';   
                            echo '</span>';
                        }else{
                            echo '<span  class="fcolor no-button judgestart" style="display:none;" id="had_collect" onclick="collect(0)">';
                            echo '    <i class="icon iconfont icon-star1"></i>已收藏';
                            echo '</span>';
                            echo '<span  class="mcolor no-button judgestart" id="no_collect" onclick="collect(1)">';
                            echo '    <i class="icon iconfont icon-star"></i>收藏';   
                            echo '</span>';
                        }
                    ?>
                    <span id="share" class="mcolor no-button" style="margin-left:36px;" onclick="share()">
                        <i class="icon iconfont icon-share"></i>分享
                    </span>
                </p>
            </div>
        </div>
        <hr>
        <div style="padding:0 10px;">
            <p style="margin:6px 0;"><strong>提要文摘附注：</strong></p>
            <div id="description" style="line-height:20px;">
                <?php if($detail_list['brief']==""){echo '暂无摘要';}else{echo $detail_list['brief'];} ?>
            </div>
        </div>
        <div class="block item" style="box-shadow:none; background:#F8F9FA;padding:0;">
            <table class="book-detail-table ">
                <thead>
                    <tr>
                        <th>馆藏地</th>
                        <th>书刊状态</th> 
                    </tr>
                </thead>
                <tbody>
                    <?php
                        foreach( $detail_table as $key1 => $val1 ){
                            echo '<tr >';
                            echo '    <td style="width:40%;">'.$val1['location'].'</td>';
                            echo '    <td style="width:58%;">';
                            echo '        <font color="green">'.$val1['state'].'</font>';
                            echo '    </td>';
                            echo '</tr>';
                        }
                    ?>
                </tbody>
            </table>    
        </div>
        <hr>
        <div style="padding:0 10px;display:none" id="related_rent_url_table">
            <p style="margin:6px 0;"><strong>相关借阅：</strong></p>
            <table class="book-detail-table ">
                <thead>
                    <tr>
                        <th>题名</th>
                        <th>责任者</th>
                        <th>出版社</th> 
                        <th>索书号</th> 
                    </tr>
                </thead>
                <tbody id="related_rent_url">
                    
                </tbody>
            </table> 
        </div>
        <hr>
        <div style="padding:0 10px;display:none" id="related_collect_url_table">
            <p style="margin:6px 0;"><strong>相关收藏：</strong></p>
            <table class="book-detail-table ">
                <thead>
                    <tr>
                        <th>题名</th>
                        <th>责任者</th>
                        <th>出版社</th> 
                        <th>索书号</th> 
                    </tr>
                </thead>
                <tbody id="related_collect_url">
                    
                </tbody>
            </table> 
        </div>
    </section>
<?php include 'footer.php';?>
<script type="text/javascript">
    $("#author a").css("color","#909090").attr("href","javascript:void(0);");
    $("#title a").attr("href","javascript:void(0);");
    $("html").css("height","auto");
    $("body").css("height","auto");


    var temp_description =  ($("#title").text()).replace(/<[^>]+>/g,"");
    var img = $("#book_img").attr("src");
    $("meta[name=description]").attr("content",temp_description);
    $("meta[name=img]").attr("content",img);

    var temp_agent = '<?php if(isset($user_agent)){ echo $user_agent; }else{ echo 0; } ?>';
    if (temp_agent=='ptimeandroid'||temp_agent=='wechat'||temp_agent=='mqq') {
        $("#share").hide();
        $(".judgestart").css("float","right");
    };

    var mac_no = '<?php if($detail_list['mac_no']==''){ echo 0; }else{echo $detail_list['mac_no'];} ?>';
    var author = '<?php if($detail_list['author']==''){ echo 0; }else{echo $detail_list['author'];} ?>';
    var title = '<?php if($detail_list['title']==''){ echo 0; }else{ $temp_title = str_replace('<a','<span',$detail_list['title']);$temp_title = str_replace('</a','</span',$temp_title); echo $temp_title; } ?>';
    var index = '<?php if(isset($detail_table[0])){echo $detail_table[0]['index'];}else{echo '0';} ?>';
    var brief = '<?php if($detail_list['brief']==''){ echo 0; }else{echo $detail_list['brief'];} ?>';
    var pic ='<?php if($detail_list['pic']==''){ echo 0; }else{echo $detail_list['pic'];} ?>';
    var press ='<?php if($detail_list['press']==''){ echo 0; }else{echo $detail_list['press'];} ?>';

    var related_rent_url = '<?php echo $related_rent_url; ?>';//相关借阅
    var related_collect_url = '<?php echo $related_collect_url; ?>';//相关收藏

    //topbar
    $("#topbar").html('<a id="goback" onclick="history.go(-1)"><h1 class="mustfff"><i class="icon iconfont icon-left" style="margin-right:4px;"></i>'+title+'</h1></a>');

    function collect(judge){
        if (mac_no==0) {toast("粗错啦~~~T T");return;};
        if (user_id==0) {location.href="/ptime/login?return_url="+window.location.href;return;};
        $("#loading").show();
        $.ajax({
            type:"post",
            url:"/api/book/collect",
            data:{"user_id":user_id,"mac_no":mac_no,"brief":brief,"index":index,"title":title,"author":author,"pic":pic,"press":press},
            dataType:"json",//text/xml/json，非必须
            success:function(data){//回调函数，非必须
                $("#loading").hide();
                if (!data.error) {
                    if (judge==0) {
                        $("#had_collect").hide();
                        $("#no_collect").show();
                        toast("已取消收藏~");
                    }else{
                        $("#had_collect").show();
                        $("#no_collect").hide();
                        toast("收藏成功~");                        
                    };
                }else{
                    toast(data.message);
                };
            }
        });     
    }
    //获取相关借阅
    $.ajax({
        type:"get",
        url:"/api/related?url="+related_rent_url+"&type=0",
        dataType:"json",//text/xml/json，非必须
        success:function(data){//回调函数，非必须
            if (data==null) {return;};
            if (!data.error) {
                var tpl='';
                for(var i=0;i<data.result.length;i++){
                    var temp1 =  (data.result[i].name).indexOf(">");
                    var temp2 =  (data.result[i].name).indexOf("/");
                    data.result[i].name = (data.result[i].name).substring(temp1+1,temp2-1);
                    console.log(data.result[i].name);
                    var temp = '<tr><td>'+data.result[i].name+'</td><td>'+data.result[i].author+'</td><td>'+data.result[i].press+'</td><td>'+data.result[i].index+'</td></tr>';
                    tpl = tpl+temp;
                }
                $("#related_rent_url").append(tpl);
                if (data.result.length!=0) {
                    $("#related_rent_url_table").show();                    
                }

            };
        }
    });  
    //获取相关收藏
    $.ajax({
        type:"get",
        url:"/api/related?url="+related_collect_url+"&type=1",
        dataType:"json",//text/xml/json，非必须
        success:function(data){//回调函数，非必须
            if (data==null) {return;}; 

            if (!data.error) {
                var tpl='';
                for(var i=0;i<data.result.length;i++){
                    var temp1 =  (data.result[i].name).indexOf(">");
                    var temp2 =  (data.result[i].name).indexOf("/");
                    data.result[i].name = (data.result[i].name).substring(temp1+1,temp2-1);
                    var temp = '<tr><td>'+data.result[i].name+'</td><td>'+data.result[i].author+'</td><td>'+data.result[i].press+'</td><td>'+data.result[i].index+'</td></tr>';
                    tpl = tpl+temp;
                }
                $("#related_collect_url").append(tpl);
                if (data.result.length!=0) {
                    $("#related_collect_url_table").show();
                };
            };
        }
    });  
 

</script>